from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from rest_framework.test import APIClient
from rest_framework import status
from datetime import datetime, timedelta
from .models import Team, TeamMember, TeamTask

class TeamTests(TestCase):
    def setUp(self):
        # 创建测试用户
        self.client = APIClient()
        self.user = User.objects.create_user(
            username='testuser',
            password='testpass123'
        )
        self.client.force_authenticate(user=self.user)
        
        # 创建测试团队
        self.team = Team.objects.create(
            name='测试团队',
            description='测试团队描述',
            leader=self.user,
            max_members=5,
            is_public=True
        )
        
        # 创建团队成员关系
        self.team_member = TeamMember.objects.create(
            team=self.team,
            user=self.user,
            role='leader',
            status='accepted',
            join_time=timezone.now()
        )

    def test_create_team(self):
        """测试创建团队"""
        response = self.client.post('/api/team/teams/', {
            'name': '新团队',
            'description': '新团队描述',
            'max_members': 5,
            'is_public': True
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Team.objects.count(), 2)

    def test_join_team(self):
        """测试加入团队"""
        # 创建另一个用户
        other_user = User.objects.create_user(
            username='otheruser',
            password='testpass123'
        )
        self.client.force_authenticate(user=other_user)
        
        response = self.client.post('/api/team/members/', {
            'team': self.team.id
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(
            TeamMember.objects.filter(
                team=self.team,
                user=other_user,
                status='pending'
            ).count(),
            1
        )

    def test_create_team_task(self):
        """测试创建团队任务"""
        response = self.client.post(f'/api/team/teams/{self.team.id}/tasks/', {
            'title': '测试任务',
            'description': '测试任务描述',
            'status': 'pending',
            'start_time': timezone.now(),
            'end_time': timezone.now() + timedelta(days=1)
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(TeamTask.objects.count(), 1)

    def test_update_team(self):
        """测试更新团队信息"""
        response = self.client.put(f'/api/team/teams/{self.team.id}/', {
            'name': '更新的团队名称',
            'description': '更新的团队描述'
        })
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.team.refresh_from_db()
        self.assertEqual(self.team.name, '更新的团队名称')

    def test_get_team_members(self):
        """测试获取团队成员列表"""
        response = self.client.get(f'/api/team/teams/{self.team.id}/members/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)
